//
//  ADLocationManager.m
//  Anda_Driver_Pro
//
//  Created by AnDa-Y on 2019/8/2.
//  Copyright © 2019 AnDa-Y. All rights reserved.
//

#import "ADLocationManager.h"
#import <AMapLocationKit/AMapLocationKit.h>

#define kLManagerSelf  [ADLocationManager sharedManager]

@interface ADLocationManager ()<AMapLocationManagerDelegate>

@property (strong, nonatomic) AMapLocationManager *amapLocationManager;
@property (strong, nonatomic) ADLocationModel *currentLocationModel;
@property (strong, nonatomic) NSMutableSet *delegateSet;

@end

@implementation ADLocationManager

+ (instancetype)sharedManager
{
    static ADLocationManager *manager = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        
        manager = [[self alloc]init];
        manager.delegateSet = [NSMutableSet set];
        
        manager.amapLocationManager = [[AMapLocationManager alloc]init];
        manager.amapLocationManager.delegate = manager;
        manager.amapLocationManager.distanceFilter = 0.01;
        manager.amapLocationManager.locatingWithReGeocode = YES;
        
        [manager.amapLocationManager setPausesLocationUpdatesAutomatically:NO];
        if (@available(iOS 9,*)) {
            [manager.amapLocationManager setAllowsBackgroundLocationUpdates:YES];
        }
    });
    return manager;
}

+ (void)initialWithAppKey:(NSString *)appKey
{
    [kLManagerSelf.amapLocationManager startUpdatingLocation];
}

+ (void)addDelegate:(id<ADLocationManagerDelegate>)delegate
{
    [kLManagerSelf.delegateSet addObject:delegate];
}

+ (void)removeDelegate:(id<ADLocationManagerDelegate>)delegate;
{
    [kLManagerSelf.delegateSet removeObject:delegate];
}

+ (ADLocationModel *)currentLocationModel
{
    return kLManagerSelf.currentLocationModel;
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    if(location.coordinate.latitude && location.coordinate.longitude){
        
        ADLocationModel *locationModel = [ADLocationModel new];
        locationModel.latitude = location.coordinate.latitude;
        locationModel.longitude = location.coordinate.longitude;
        locationModel.speed = location.speed;
        locationModel.angle = location.course;
        locationModel.time = [location.timestamp timeIntervalSince1970] * 1000;
        locationModel.type = ADLocationTypeLocation;
        locationModel.accuracy = location.horizontalAccuracy;
        
        locationModel.adcode = reGeocode.adcode;
        locationModel.province = reGeocode.province;
        locationModel.city = reGeocode.city;
        locationModel.name = reGeocode.POIName;
        locationModel.address = reGeocode.formattedAddress;
        
        NSError *error = nil;
        if (!reGeocode.adcode.length) {
            error = [NSError errorWithDomain:NSCocoaErrorDomain code:-1000 userInfo:@{NSLocalizedDescriptionKey:@"反地理编码失败"}];
        }
        
        if (!error) {
            self.currentLocationModel = locationModel;
            for (id<ADLocationManagerDelegate> delegate in self.delegateSet) {
                [delegate locationManagerLocationUpdate:locationModel];
            }
        } else {
            NSLog(@"%@",error);
        }
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager *)locationManager
{
    [locationManager requestAlwaysAuthorization];
}

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"定位失败");
}

@end
